package cn.rui.service.impl;

import cn.rui.entity.PageResult;
import cn.rui.entity.QueryPageBean;
import cn.rui.mapper.MemberMapper;
import cn.rui.mapper.OrderMapper;
import cn.rui.pojo.Member;
import cn.rui.service.MemberService;
import cn.rui.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 徽州大都督
 * @date 2020/9/1
 */
@Service
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage ();
        Integer pageSize = queryPageBean.getPageSize ();
        String queryString = queryPageBean.getQueryString ();

        PageHelper.startPage (currentPage, pageSize);

        Page<Member> page = memberMapper.selectByCondition (queryString);

        long total = page.getTotal ();
        List<Member> memberList = page.getResult ();



        PageResult pageResult = new PageResult (total, memberList);

        return pageResult;
    }

    /**
     * 新增会员
     * @param member
     */
    public void add(Member member) {
        member.setRegTime (new Date ());
        memberMapper.add(member);
    }

    /**
     * 根据id删除会员
     * @param id
     */
    public void deleteById(Integer id) {
        Member member = memberMapper.findById (id);

        //并删除此会员所有预约信息
        orderMapper.deleteByMemberId (member.getId ());
        memberMapper.deleteById(id);


    }
}
